//
//  RtmpSetViewController.swift
//  onemeetingmac
//
//  Created by 张衡 on 2020/6/30.
//  Copyright © 2020 onemeeting. All rights reserved.
//

import Foundation

class RtmpSetViewController: BaseTRTCViewController {
    
    override func loadView() {
        let view = NSView.init(frame: NSMakeRect(0, 0, kSet_LiveWidth, kSet_BasicHeight))
        view.wantsLayer = true
        view.layer?.backgroundColor = color_gray(246).cgColor
        self.view = view
    }
    
    
    override func viewWillAppear() {
        super.viewWillAppear()
        self.view.window?.title = "直播设置"
    }
     
    override func viewDidLoad() {
        super.viewDidLoad()
        self.setupUI()
        self.loadData()
    }
     
    func setupUI()  {
        self.view.addSubview(webView)
        webView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview().inset(NSEdgeInsets.init(top: 0, left: 0, bottom: 0, right: 0))
        }
         
    }
     
    func loadData()  {
        self.load(url: URL.init(string:mt_url_settings)!)
    }
     
     
    deinit {
        self.webView.configuration.userContentController.removeScriptMessageHandler(forName: "refresh")
        self.webView.configuration.userContentController.removeScriptMessageHandler(forName: "liveshare")
        self.webView.configuration.userContentController.removeScriptMessageHandler(forName: "download")
    }
    
     
     
    lazy var webView: WKWebView = {
        let config = WKWebViewConfiguration( )
        let preferences = WKPreferences()
        preferences.javaScriptEnabled = true
        config.preferences = preferences
        let userctrl = WKUserContentController()
        config.userContentController = userctrl
         
        let web = WKWebView.init(frame: NSRect.zero, configuration: config)
        web.translatesAutoresizingMaskIntoConstraints = false
        web.wantsLayer = true
        web.layer?.backgroundColor = color_gray(246).cgColor
        return web
    }()
     
     func load(url:URL) {
        if let userid = Td.sh().params.userId {
            self.accessToken { (ciphertext) in
                let urlstring = "\(mt_url_omappsetup)?ownid=\(userid)&token=\(ciphertext)"
                self.webView.load(URLRequest.init(url: URL.init(string: urlstring)!))
            }
            
        }
     }
    
    func accessToken(seccess:((String)->())?=nil) {
        if let ownid = UserInfoManager.shared.ownid {
            let params = ["plaintext":ownid]
            NetworkTool.requestData(type: MethodType.post, URLString: mt_url_accessToken, parameters: params, finishCallback: { (result) in
                if let dict = result as? [String:Any] {
                    if let data = dict["data"] as? [String:Any] {
                        if let ciphertext = data["ciphertext"] as? String {
                            if seccess != nil {
                                seccess!(ciphertext)
                            }
                        }
                    }
                }
                
            }) { (error) in
                
            }
        }
    }
    
    
}
